/**
 * @author Eddie
 * @date 2023-10-22
 * 课程相关接口(对外提供，例如大屏)
 */

const router = require('koa-router')();
const fs = require('fs');

// 定义路由前缀，没有可不添加
router.prefix('/api/class');

// 添加康乐活动
router.post('/addKlhd', async (ctx, next) => {
  let data = ctx.request.body;
  let re = {};

  try {
    fs.writeFileSync('./files/klhd.json', JSON.stringify(data));
    console.log("写入 klhd.js 成功");

    re = {
      code: 1,
      msg: "写入成功"
    }
  } catch (error) {
    console.log("写入失败" + error);

    re = {
      code: -1,
      msg: "写入失败" + error
    }
  }

  ctx.body = re;
})

// 获取康乐活动信息
router.get('/getKlhd', async (ctx, next) => {
  try {
    let klhdObj = fs.readFileSync('./files/klhd.json','utf8');
    ctx.body = {
      code : 1,
      msg: "读取成功",
      data : JSON.parse(klhdObj)
    }
  } catch (error) {
    console.log("getKlhd", error);
    ctx.body = {
      code : -1,
      msg: "读取失败：" + error,
      data : {}
    }
  }
})

// 添加康复课程
router.post('/addKfkc', async (ctx, next) => {
  let data = ctx.request.body;
  let re = {};

  try {
    fs.writeFileSync('./files/kfkc.json', JSON.stringify(data));
    console.log("写入 kfkc.js 成功");

    re = {
      code: 1,
      msg: "写入成功"
    }
  } catch (error) {
    console.log("写入失败" + error);

    re = {
      code: -1,
      msg: "写入失败" + error
    }
  }

  ctx.body = re;
})

// 获取康复课程
router.get('/getKfkc', async (ctx, next) => {
  try {
    let kfkcObj = fs.readFileSync('./files/kfkc.json','utf8');
    ctx.body = {
      code : 1,
      msg: "读取成功",
      data : JSON.parse(kfkcObj)
    }
  } catch (error) {
    console.log("getKfkc", error);
    ctx.body = {
      code : -1,
      msg: "读取失败：" + error,
      data : {}
    }
  }
})

module.exports = router;